#coding=utf-8
'''
Created on 2017-07-15

@author: sandy
'''
import logging

class Table(object):
    def __init__(self):
        self.person = 0
        self.round = 0
        self.card = 0

    def parse(self,js):
        self.person = js.get("person")
        self.round = js.get("round")
        self.card = js.get("card")

class Model(object):
    def __init__(self):
        self.index = 0
        self.model = 0

    def parse(self,js):
        self.index = js.get("index")
        self.model = js.get("model")
        

class CreateTableConfig(object):
    
    def __init__(self):
        self.__tableMap = {}
        self.__modelMap = {}
        
    def onRemoteConfig(self, cfgname, jf):
        if cfgname!= u"clientconfig.createtable":
            #logging.info("cfgname = %s",cfgname); 
            return
        self.loadFromJson(jf)
            
    def loadFromJson(self, js):
        logging.debug("!!!!!!!!!!!!!!clientconfig.createtable loadFromJson")
        jf = js.get("table")
        for i in range(len(jf)):
            t = jf[i]
            cf = Table()
            cf.parse(t)
            if self.__tableMap.get(cf.person,None) == None:
                self.__tableMap[cf.person] = []
            self.__tableMap[cf.person].append(cf)

        jf = js.get("playmodel")
        for i in range(len(jf)):
            m = jf[i]
            cf = Model()
            cf.parse(m)
            self.__modelMap[cf.index] = cf
            
        
    def checkTableModel(self,tableModel):
        logging.debug("checkTableModel tableModel=%d mapkeys = %s",tableModel,str(self.__tableMap.keys()))
        ok = tableModel in self.__tableMap.keys()
        return  ok
    
    def checkMaxRound(self,person,round):
        configs = self.__tableMap.get(person)
        rounds = []
        for i in range(len(configs)):
            rounds.append(configs[i].round)
        ok =  round in rounds     
        return ok
    
    def getNeedCards(self,person,round):      
        configs = self.__tableMap.get(person)
        for i in range(len(configs)):
            if configs[i].round == round:
                return configs[i].card
        return -1

    def checkPlayModel(self,playmodel):
        for key in self.__modelMap:
            model = self.__modelMap.get(key)
            if model.model == playmodel:
                return True
        return False
    
instance = CreateTableConfig()

if __name__ == '__main__':
    pass